<?php

/**
 * Empresa form base class.
 *
 * @method Empresa getObject() Returns the current form's model object
 *
 * @package    cronos-doctrine
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseEmpresaForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'          => new sfWidgetFormInputHidden(),
      'padre'       => new sfWidgetFormInputText(),
      'entidad_id'  => new sfWidgetFormInputText(),
      'nombre'      => new sfWidgetFormInputText(),
      'rif'         => new sfWidgetFormInputText(),
      'logo'        => new sfWidgetFormInputText(),
      'correo'      => new sfWidgetFormInputText(),
      'correo_inf'  => new sfWidgetFormInputText(),
      'expires_at'  => new sfWidgetFormDateTime(),
      'iva'         => new sfWidgetFormInputText(),
      'feriados'    => new sfWidgetFormChoice(array('choices' => array(1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7'))),
      'created_at'  => new sfWidgetFormDateTime(),
      'updated_at'  => new sfWidgetFormDateTime(),
      'idioma_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Idioma')),
      'moneda_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Moneda')),
      'pago_list'   => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Pago')),
    ));

    $this->setValidators(array(
      'id'          => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'padre'       => new sfValidatorInteger(array('required' => false)),
      'entidad_id'  => new sfValidatorInteger(array('required' => false)),
      'nombre'      => new sfValidatorString(array('max_length' => 255)),
      'rif'         => new sfValidatorString(array('max_length' => 25)),
      'logo'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'correo'      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'correo_inf'  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'expires_at'  => new sfValidatorDateTime(),
      'iva'         => new sfValidatorNumber(array('required' => false)),
      'feriados'    => new sfValidatorChoice(array('choices' => array(0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5', 5 => '6', 6 => '7'), 'required' => false)),
      'created_at'  => new sfValidatorDateTime(),
      'updated_at'  => new sfValidatorDateTime(),
      'idioma_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Idioma', 'required' => false)),
      'moneda_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Moneda', 'required' => false)),
      'pago_list'   => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Pago', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('empresa[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Empresa';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['idioma_list']))
    {
      $this->setDefault('idioma_list', $this->object->Idioma->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['moneda_list']))
    {
      $this->setDefault('moneda_list', $this->object->Moneda->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['pago_list']))
    {
      $this->setDefault('pago_list', $this->object->Pago->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveIdiomaList($con);
    $this->saveMonedaList($con);
    $this->savePagoList($con);

    parent::doSave($con);
  }

  public function saveIdiomaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['idioma_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Idioma->getPrimaryKeys();
    $values = $this->getValue('idioma_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Idioma', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Idioma', array_values($link));
    }
  }

  public function saveMonedaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['moneda_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Moneda->getPrimaryKeys();
    $values = $this->getValue('moneda_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Moneda', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Moneda', array_values($link));
    }
  }

  public function savePagoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['pago_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Pago->getPrimaryKeys();
    $values = $this->getValue('pago_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Pago', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Pago', array_values($link));
    }
  }

}
